#!/usr/bin/python

L = [123, 'spam', 1.23]
print L
print len(L)

for i in range(len(L)):
	print "L[" + str(i) + "] = " + str(L[i])
	
print L[0]
L[0] = 'new-new-new'
print L

print "L[:-1] = ", L[:-1]
print "L + [4,5,6] = ", L + [4,5,6]
print L

L.append("NI")
print L
print "L.pop() = ", L.pop()
print L
print "L.pop(0) = ", L.pop(0)
print L

print "del L[1]"
del L[1]
print L

del L
try:
	print L
except Exception, e:
	print e

L=range(11)
print "list.insert(pos,obj) test:"
print L
L.insert(1,9000)
print L
L.insert(0, 9000)
print L
L.insert(len(L), 9000)
print L

print "list.remove(obj-value) test:"
L.remove(9000)
print L
L.remove(9000)
print L
L.remove(9000)
print L

print "list.reverse and list.sort test"
L=[2,3,4,1,5]
print L
L.reverse()
print L
L.sort()
print L

print "IndexErrors"
try:
	print L[9000]
except Exception, e:
	print e

try:
	L[100500] = 123
except Exception, e:
	print e

# Nested lists
print "The Matrix:"
matrix = [[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]]
print matrix
for i in range(3):
	print "matrix[" + str(i) + "] = ", matrix[i]

for i in range(3):
	for j in range(3):
		print "matrix[%d][%d] = %s" % (i, j, str(matrix[i][j]))

# List generators
# [expression_with_x for x in sequence]
# [expression_with_x for x in sequence if conditional_expression_with_x]
print "Get Matrix Columns via list generator:"
print "column 1 = ", [row[1] for row in matrix]
print("Colums:")
for i in range(3):
	print [row[i] for row in matrix]

print [row[1] * 10 for row in matrix]
print [row[1] for row in matrix if row[1] % 2 == 0]
print [matrix[i][i] for i in [0,1,2]]
print [c*2 for c in 'spam']

# map function
# NOTE: In Python 3 map it's a class instead of a function like in Python 2
from math import hypot
print map(hypot, [0, 1, 2], [0, -1, 3])
pairs = [(1,2), (3,4), (5,6)]
xs = [x[0] for x in pairs]
ys = [x[1] for x in pairs]
print pairs
print xs
print ys
print map(hypot, xs, ys)

# filter function
# NOTE: In Python 3 filter it's a class instead of a function like in Python 2
print "filter"
print "None-Zero its True"
print filter(None, [0,1,0,2,0,3,0])
print filter(lambda x: True if x % 2 == 0 else False, [0,1,2,3,4,5,6,7,8,9,10])
print filter(lambda x: x % 2 == 0, range(11))

# Generator object
l = [1,2,3,4,5]
print l
gen = (x**2 for x in l)
try:
	while 1:
		print next(gen)
		print gen.next()
except Exception, e:
	print repr(e)

# set and dict generators
print l
print {x**2 for x in l}
print {x:x**2 for x in l}

ts = "spaaam"
print ts
print [ord(x) for x in ts]
print {ord(x) for x in ts}
print {x:ord(x) for x in ts}
